﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

//InternationalizedColLocEditor
namespace SdmxMl.UiEditors
{
    /// <summary>
    /// Custom UI Type editor for defining column size in Table row.
    /// The size are held in an integer array.
    /// </summary>
    public class InternationalizedColLocEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(
            ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        /// <summary>
        /// Provide our editor for multiline text
        /// </summary>
        /// <param name="context"> Information about the context</param>
        /// <param name="provider"> Mechanism for rerieving service object</param>
        /// <param name="value"> actual property value </param>
        /// <returns> edited text </returns>
        public override object EditValue(ITypeDescriptorContext context,
            IServiceProvider provider, object value)
        {
            // Attempts to obtain an IWindowsFormsEditorService.
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)
                provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc == null || value == null)
                return value;

            int[] arr = value as int[];
            UcMultiLang ucm = new UcMultiLang(ref arr);
            ucm.Dock = DockStyle.Fill;
            ucm._wfes = edSvc;
            edSvc.DropDownControl(ucm);
            value = ucm.ArrLoc;
 
       //     bool bkeyArray = context.PropertyDescriptor.Converter is DicoColConverter;

            // Instantiate our form property editor

            /*
            GridForm frm = new GridForm(MakeTable(value as int[], "Column Index"), false, false);
            frm.Title = "Specifies column for each language";
           // frm._wfes = edSvc;

            // Propose dialog
           // edSvc.DropDownControl(frm);
            
            if (edSvc.ShowDialog(frm) == DialogResult.OK)
            {
                // Compose item list as semi column separated
                DataTable t = frm.Content;
                int[] arr = new int[t.Rows.Count];
                int ndx = 0;
                foreach (DataRow row in t.Rows)
                        arr[ndx++] = Convert.ToInt32(row[1]);
                value = arr;
            }
            // Clear ressources
            frm.Dispose();
             * */
            

            // new (or same) property value
            return value;
        }
        /*
        // Construct table from int array
        protected virtual DataTable MakeTable(int[] arr, string col2Name)
        {
            DataTable tu = new DataTable();
            DataColumn c = new DataColumn("Language", typeof(string));

            // Limit to supported languages
            Array.Resize(ref arr, SdmxMl.Common.LangSupport.Count);
            c.ReadOnly = true;
            
            tu.Columns.Add(c);

            // Add integer value
            tu.Columns.Add(new DataColumn(col2Name, typeof(int)));
            int ndx = 0;
                for (int ndx = 0; ndx < arr.Length; ndx++)
                    tu.Rows.Add(new object[] { SdmxMl.Common.LangSupport.arrLang[ndx], arr[ndx] });
            return tu;
        }
         */
    }
}
